package ru.st.selenium.pages;


import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;



import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;




public class FilmListPage extends InternalPage{
	public FilmListPage(PageManager pages) {
		super(pages);
	}

	
 @FindBy(className = "title")
 private WebElement titleFilm;
 
 @FindBy(className = "title")
 private List<WebElement> titleFilmList;
	
  @FindBy(id = "q")
  private WebElement searchField;

  @FindBy(className = "content")
  private WebElement contentField;
  
  @FindBy(id = "results")
  private WebElement resultField;
  
public String getTitleFilm(){
	return titleFilm.getText();
}

public String getContent(){
	return contentField.getText();
}

public String getResult(){
	return resultField.getText();
}
public List<String> getListTitleFilm(){
	List<String> FilmTitles = new ArrayList<String>();
	List<WebElement> FilmTitleElement = titleFilmList;
	if (!getResult().equals("No movies where found.")){
		for(int i=0;i< FilmTitleElement.size(); i++){
			FilmTitles.add(i, FilmTitleElement.get(i).getText());
		}
	}
	return FilmTitles;
}

public FilmListPage setSearchFilm(String SearchFilmTitle) throws InterruptedException {
	searchField.sendKeys(Keys.CONTROL + "a");
    Thread.sleep(1000);
    searchField.sendKeys(Keys.DELETE);
    searchField.sendKeys(SearchFilmTitle+Keys.RETURN);
    return this;

}

public FilmViewPage clickFilm(){
	titleFilm.click();
	return pages.FilmViewPage;
}

public FilmListPage ensurePageLoaded() {
    super.ensurePageLoaded();
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
    wait.until(visibilityOfElementLocated(By.id("results")));
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    return this;
  }

}